package edu.cornell.cubooks.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.OrderBy;

import org.apache.commons.lang.time.DateUtils;

import edu.cornell.cubooks.CUBooksConstants;

import static edu.cornell.cubooks.CUBooksConstants.*;

/**
 * Business object which represents posting.
 * 
 * @author CUBooks Team
 *
 */
@Entity
@Table(name="posting")
public class Posting implements Serializable {
	
	private static final long serialVersionUID = -7745714362066479872L;	
   
	@Id 
    @GeneratedValue
	private Integer id;
    private String title;
    @Column(length=65535)
    private String description;
    private float price;
    private int status = STATUS_OPEN;
    
    @Column(name="start_date")
    private Date startDate;
    
    private Integer duration;
	@Column(name="end_date")
    private Date endDate;
    
    // many postings belong to user
    @ManyToOne
    @JoinColumn(name="user_id")
    public User user;
   
    // many postings belong to user
    @ManyToOne
    @JoinColumn(name="winner_id")
    public User winner;

    // posting has many bidsL
	@OneToMany(mappedBy="bid.posting", fetch=FetchType.EAGER)
	@OrderBy("bid.price desc")
    public Set<Bid> bids;
	
	// one posting has one feedback
	@OneToOne(mappedBy = "posting", fetch=FetchType.LAZY)
    public Feedback feedback;
	
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title.trim();
    }

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description.trim();
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public Integer getDuration() {
		return duration;
	}
	
	public void setDuration(Integer duration) {
		this.duration = duration;
	}
	
	public User getWinner() {
		return winner;
	}

	public void setWinner(User winner) {
		this.winner = winner;
	}

	public Date getStartDate() {
		return startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
		if(getDuration() == 0) {
			this.setEndDate(DateUtils.addMinutes(startDate, CUBooksConstants.TEST_START_AUCTION_MIN));
		} else {
			this.setEndDate(DateUtils.addDays(startDate, getDuration()));
		}
	}

	public Feedback getFeedback() {
		return feedback;
	}

	public void setFeedback(Feedback feedback) {
		this.feedback = feedback;
	}

	public Set<Bid> getBids() {
		return bids;
	}

	public void setBids(Set<Bid> bids) {
		this.bids = bids;
	}
}
